<template>

 <div>
    <div class="back">
        <span style="font-size: 20px; font-weight: 500; color: #2f343e"
          >规则标签</span
        >
      </div>
   <div class="total-box">
     <div class="head-container" >
            <el-tree
            style="height:100%"
              :data="deptOptions"
              :props="defaultProps"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="tree"
              highlight-current
              @node-click="handleNodeClick"
            />
          </div>
    <div class="app-container">
      <el-row :gutter="20" style="height:100%">

        <!--用户数据-->
        <el-col :span="24" :xs="24">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="关键字：" prop="userName">
              <el-input
                v-model="queryParams.userName"
                placeholder="标签名称"
                clearable
                style="width: 240px"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
  
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            </el-form-item>
          </el-form>
  
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
  
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
              >新增自定义标签</el-button>
            </el-col>
  
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" ></right-toolbar>
          </el-row>
  
          <el-table v-loading="loading" :data="userList" :header-cell-style="{ background: '#F3F5FC' }" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column   label="序号" width="50">
              <template slot-scope="scop">
                {{scop.$index+1}}
              </template>
            </el-table-column>
            <el-table-column label="标签名称"  prop="labelName"   :show-overflow-tooltip="true" />
            <el-table-column label="状态"  key="enable" width="200" >
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.enable"
                  active-value="0"
                  inactive-value="1"
                  @change="handleStatusChange(scope.row)"
                ></el-switch>
              </template>
            </el-table-column>
              <el-table-column label="标签分析方式" width="100"   prop="labelSource" :show-overflow-tooltip="true"/>
            <el-table-column
              label="操作"
              align="center"
              width="180"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope" v-if="scope.row.userId !== 1">
                <el-button
                  size="mini"
                  type="text"
                     @click="handleUpdate(scope.row)"
                >编辑</el-button>
                <el-button
                  size="mini"
                  type="text"
               @click="handleDelete(scope.row)"
                >删除</el-button>
                <el-button
                  size="mini"
                  type="text"
              
                  @click="handleRoster(scope.row)"
                >白名单</el-button>
              </template>
            </el-table-column>
          </el-table>
  
          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </el-row>
  
    <el-dialog
       title="白名单"
       :visible.sync="roster"
       width="1000px"
       :modal-append-to-body="false"
       :close-on-click-modal="false"
       class="dia-style"
     >
      <div style="padding: 0 20px 20px 0">
        <rosterDialog></rosterDialog>
      </div>
       <!-- 底部 -->
       <div slot="footer">
         <el-button @click="roster = false">取 消</el-button>
         <el-button type="primary" @click="roster = false"
           >保 存</el-button
         >
       </div>
     </el-dialog>
  
  
  
    <el-dialog
        v-if="updateOpen"
       title="修改"
       :visible.sync="updateOpen"
       width="1000px"
       :modal-append-to-body="false"
       :close-on-click-modal="false"
       class="dia-style"
  
     >
      <div style="padding: 0 20px 20px 0">
        <updadteDialog @handleUpdateProcessClick="handleUpdateProcessClick"></updadteDialog>
      </div>
  
  
       <!-- 底部
       -->
     </el-dialog>
  
  
  
  
      <el-dialog
        title="添加"
        :visible.sync="open"
        width="1000px"
        height="500px"
        :modal-append-to-body="false"
        :close-on-click-modal="false"
        class="dia-style"
      >
        <div style="padding: 0 20px 20px 0">
          <addDialog></addDialog>
        </div>
        <!-- 底部 -->
        <div slot="footer">
          <el-button @click="open = false">取 消</el-button>
          <el-button type="primary" @click="handleProcessClick"
            >保 存</el-button
          >
        </div>
      </el-dialog>
      <stepDialog ref="editRef" :title="title"/>
    </div>
   </div>

 </div>


</template>

<script>
import rulesData from './mockData/labelClass'
import accData from './mockData/accClass'
import  treeselect  from './mockData/labelTree'
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

import addDialog from './component/repositoryPage.vue'
import stepDialog from './component/emitDialog.vue'

import updadteDialog from './component/updateDialog.vue'

import rosterDialog from './roster.vue'

export default {
  provide(){
        return{
          handleUpdateProcessClick:this.handleUpdateProcessClick
        }
  },
  name: "User",
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  components: { Treeselect,addDialog,stepDialog,updadteDialog,rosterDialog},
  data() {
    return {
      // 遮罩层
      loading: true,
      open: false,
      isOpen:false,
      updateOpen:false,
      isUpdate:false,
      roster:false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 部门名称
      deptName: undefined,
      // 默认密码
      initPassword: undefined,
      // 日期范围
      dateRange: [],
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "title"
      },


      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        phonenumber: undefined,
        status: undefined,
        deptId: undefined
      },
      // 表单校验
      rules: {



      }
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    this.getList();
    this.getTreeselect();

  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      this.loading = false
      this.userList = rulesData.page.list
      this.total = rulesData.page.totalCount


    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
     this.deptOptions = treeselect;

    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      if (data.id=="host")
      {
        this.loading = true;
        this.loading = false
        this.userList = rulesData.page.list
        this.total = rulesData.page.totalCount
        return;
      }
  if (data.id=="account")
      {
        this.loading = true;
        this.loading = false
        this.userList = accData.page.list
        this.total = accData.page.totalCount
        return;
      }


        this.loading = true;
        this.loading = false
        this.userList = "";
        this.total = "";


    },
    handleProcessClick() {
      this.open=false
      this.$refs.editRef.isOpen=true;
      this.$refs.editRef.active=0;
      this.title='添加'
    },
    handleUpdateProcessClick() {
      this.updateOpen=false
      this.title='修改'
    },

    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === "1" ? "启用" : "停用";
      this.$modal.confirm('确认要' + text   + '此标签吗？').then(function() {
        return changeUserStatus(row.userId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        email: undefined,
        sex: undefined,
        status: "0",
        remark: undefined,
        postIds: [],
        roleIds: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleResetPwd":
          this.handleResetPwd(row);
          break;
        case "handleAuthRole":
          this.handleAuthRole(row);
          break;
        default:
          break;
      }
    },
    /** 新增按钮操作 */
    handleAdd() {

      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
     this.updateOpen = true;
     this.title = "添加角色";
    },

    /** 修改按钮操作 */
    handleRoster(row) {
     this.roster = true;
     this.title = "白名单";
    },

    /** 重置密码按钮操作 */
    handleResetPwd(row) {
      this.$prompt('请输入"' + row.userName + '"的新密码', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputPattern: /^.{5,20}$/,
        inputErrorMessage: "用户密码长度必须介于 5 和 20 之间"
      }).then(({ value }) => {
          resetUserPwd(row.userId, value).then(response => {
            this.$modal.msgSuccess("修改成功，新密码是：" + value);
          });
        }).catch(() => {});
    },
    /** 分配角色操作 */
    handleAuthRole: function(row) {
      const userId = row.userId;
      this.$router.push("/system/user-auth/role/" + userId);
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != undefined) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除此数据项？').then(function() {
        return delUser(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/user/importTemplate', {
      }, `user_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
<style lang="scss" scoped>
.total-box{
  display: flex;
  width: 100%;
  height: 100%;
}
.head-container{
  overflow: auto;
  padding: 20px;
  height: 800px;
width: 270px;
border-right: 1px solid rgba(0, 0, 0, 0.08);
 
}
.app-container{
  flex: 1;
  height: 800px;
}
.back {
  margin-bottom: 20px;
}
.content-box {
  background-color: #ffffff;
  //   height: 1000px;
  padding-left: 100px;
  padding-top: 50px;
  padding-bottom: 50px;
}
::v-deep .head-container[data-v-3ee8866c] {
    background-color: #FFFFFF;
}
</style>